package com.umcg.monitor;

import org.json.JSONArray;

import android.app.Dialog;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SettingsDialog {
	private int line1 = 80;
	private int line2 = 20;
	
	private CheckBox checkbox;
	private Dialog dialog;
	private SeekBar seekbar1;
	private SeekBar seekbar2;
	private String title;
	private WebView webview;
	private TextView alarm_high;
	private TextView alarm_low;	
	private Button cancel;
	private Button ok;

	public SettingsDialog(Dialog dialog) {
		this.dialog = dialog;
		dialog.setContentView(R.layout.settings_dialog);
		setup();
		
	}
	
	public void setTitle(String title){
		this.title=title;
		dialog.setTitle(title+ "                                                                                                         ");
	}
	
	private void setup(){
		
		webview = (WebView)dialog.findViewById(R.id.webViewSettingsDialog);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.addJavascriptInterface(this, "testhandler");
		webview.loadUrl("file:///android_asset/flot/plot/settings_dialog.html");
		webview.setBackgroundColor(0);
		
		checkbox = (CheckBox)dialog.findViewById(R.id.checkBoxSettingsDialog);
		alarm_high = (TextView) dialog.findViewById(R.id.alarm_high);
		alarm_low = (TextView) dialog.findViewById(R.id.alarm_low);
		cancel = (Button) dialog.findViewById(R.id.cancel);
		ok = (Button) dialog.findViewById(R.id.ok);
		
		seekbar1 = (SeekBar)dialog.findViewById(R.id.seekBarSettingsDialog1);
		seekbar2 = (SeekBar)dialog.findViewById(R.id.seekBarSettingsDialog2);
		seekbar1.setOnSeekBarChangeListener(mySeekbarListener1);
		seekbar2.setOnSeekBarChangeListener(mySeekbarListener2);
		seekbar1.setProgress(line1);
		seekbar2.setProgress(line2);
		
		cancel.setOnClickListener(new View.OnClickListener() {
                			
			@Override
			public void onClick(View view) {
				dialog.cancel();
				
			}
		});
		
		ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View view) {
				/*
				 * Hier moeten de alarmen op geslagen worden
				 * ipv gecanceld
				 * 
				 */
				dialog.cancel();
				
			}
		});
	}
	

	public void show() {
		dialog.show();
	}


	private OnSeekBarChangeListener mySeekbarListener1 = new OnSeekBarChangeListener() {
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromTouch) {
			alarm_high.setText("" + progress);
			if (fromTouch) {
				if (checkbox.isChecked()) {
					if (line2 + (progress - line1) >= 0) {
						line2 = line2 + (progress - line1);
						seekbar2.setProgress(line2);
					}
				}
				if (line1 < line2) {
					line2 = line1;
					seekbar2.setProgress(line2);
				}
				line1 = progress;
				//setData(line1, line2);
				plotData(0);
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}
	};

	private OnSeekBarChangeListener mySeekbarListener2 = new OnSeekBarChangeListener() {
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromTouch) {
			alarm_low.setText("" + progress);
			if (fromTouch) {
				if (checkbox.isChecked()) {
					if (line1 + (progress - line2) >= 0) {
						line1 = line1 + (progress - line2);
						seekbar1.setProgress(line1);
					}
				}
				if (line1 < line2) {
					line1 = line2;
					seekbar1.setProgress(line1);
				}
				line2 = progress;
				// graphHandler.setData(line1, line2);
				plotData(0);
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
	};
	
	/**
	 * Creates the JSONArray necessary to draw a line.
	 * 
	 */
	private JSONArray series(int line, int dataPoints) {
		JSONArray series = new JSONArray();
		for (int i = 0; i < dataPoints; i++) {
			JSONArray points = new JSONArray();
			points.put(i);
			points.put(line);
			series.put(points);
		}
		return series;
	}
	
	public void plotData(int i) { 
		JSONArray series1 = series(line1, 2);
		JSONArray series2 = series(line2, 2);
		JSONArray series3 = series(50, 2);
		
		if (i==1) {
			webview.loadUrl("javascript:setData(" + series1.toString() + "," + series2.toString() + "," + series3.toString() + ")");
		} else {
			webview.loadUrl("javascript:update("+ series1.toString() + "," + series2.toString() + "," + series3.toString() + ")");
		}
	}
	
	public void setData(int line1, int line2){
		this.line1 = line1;
		this.line2 = line2;
	}
}